using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class UserPostUploadVoteTotal : BaseItem
    {
        private UserPostUploadVoteTotal() : base() { }
        private UserPostUploadVoteTotal(Hashtable table) : base(table) { }
        private UserPostUploadVoteTotal(Hashtable table, List<UserPostUploadVoteTotal> collection) : base(table, collection) { }

        public static UserPostUploadVoteTotal CreateInstance() { return new UserPostUploadVoteTotal(); }

        protected override Base CreateDal()
        {
            return new UserPostUploadVoteTotalDal();
        }

        private static UserPostUploadVoteTotal Get(Hashtable table)
        {
            if (table != null)
            {
                return new UserPostUploadVoteTotal(table);
            }
            else
            {
                return null;
            }
        }

        private static List<UserPostUploadVoteTotal> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<UserPostUploadVoteTotal> tmp = new List<UserPostUploadVoteTotal>();
                foreach(Hashtable table in tables)
                {
                    tmp.Add(new UserPostUploadVoteTotal(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static List<UserPostUploadVoteTotal> GetAllByUploadId(int uploadId)
        {
            UserPostUploadVoteTotalDal dal = new UserPostUploadVoteTotalDal();
            return Get(dal.GetAllByUploadId(uploadId));
        }

        protected override void Load(Hashtable table)
        {
            postId = (int)table[UserPostUploadVoteTotalDal.Fields.PostId];
            uploadId = (int)table[UserPostUploadVoteTotalDal.Fields.UploadId];
            score = (decimal)table[UserPostUploadVoteTotalDal.Fields.Score];
            total = (int)table[UserPostUploadVoteTotalDal.Fields.Total];
        }

        private int postId;
        public int PostId
        {
            get { return postId; }
        }


        private int uploadId;
        public int UploadId
        {
            get { return uploadId; }
        }

        private decimal score;
        public decimal Score
        {
            get { return score; }
        }

        private int total;
        public int Total
        {
            get { return total; }
        }

        public decimal Percentage
        {
            get
            {
                if (Collection != null)
                {
                    int allVotes = 0;
                    foreach (UserPostUploadVoteTotal item in base.Collection)
                    {
                        allVotes += item.Total;
                    }
                    if (allVotes > 0)
                    {
                        return (decimal)total / (decimal)allVotes;
                    }
                }
                return 0M;
            }
        }


        public static void Sort(List<UserPostUploadVoteTotal> tmp, string expression)
        {
            BaseItem.SortList(tmp, expression, new UserPostUploadVoteTotalComparison());
        }

    }

    internal class UserPostUploadVoteTotalComparison : BaseComparison<UserPostUploadVoteTotal>
    {
        public override int Compare(UserPostUploadVoteTotal x, UserPostUploadVoteTotal y)
        {
            switch (base.Field.ToString())
            {
                case "score":
                    return GetReturnValue(x.Score, y.Score);
                case "total":
                    return GetReturnValue(x.Total, y.Total);
            }
            return 0;
        }
    }
}
